package com.k.note;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

/**
 * Created by mac_wk on 14-11-26.
 */
public class SwipeMenuListView extends ListView {
    private SwapLin mTouchView;
    public SwipeMenuListView(Context context) {
        super(context);
        init(context);
    }
    public SwipeMenuListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SwipeMenuListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.L)
    public SwipeMenuListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }
    private void init(Context context) {

    }
    private float mDownX;
    private float mDownY;
    private int mTouchState;
    private int mTouchPosition;
    private static final int TOUCH_STATE_NONE = 0;
    private static final int TOUCH_STATE_X = 1;
    private static final int TOUCH_STATE_Y = 2;
    private int MAX_Y = 5;
    private int MAX_X = 3;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //当不是按下动作并且touchview为null的时候
        if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null)
            return super.onTouchEvent(ev);
        int action = MotionEventCompat.getActionMasked(ev);
        action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                int oldPos = mTouchPosition;
                mDownX = ev.getX();
                mDownY = ev.getY();
                mTouchState = TOUCH_STATE_NONE;
                //获取到当前按下的position
                mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
                //判断当前的position是否跟之前的相同，如果相同并且toucheview不为null 并且为打开状态则关闭之前的
                if (mTouchPosition == oldPos && mTouchView != null
                        && mTouchView.isOpen()) {
                    mTouchState = TOUCH_STATE_X;
//                    mTouchView.onSwipe(ev);
                    return true;
                }

                //获取到当前滑动的view
                View view = getChildAt(mTouchPosition - getFirstVisiblePosition());

                if (mTouchView != null && mTouchView.isOpen()) {
                    //关闭当前滑动的view
//                    mTouchView.smoothCloseMenu();
                    mTouchView = null;
                    return super.onTouchEvent(ev);
                }
                //如果当前的view是SwipeMenuLayout
                if (view instanceof SwipeMenuLayout) {
                    mTouchView = (SwapLin) view;
                }
                if (mTouchView != null) {
//                    mTouchView.onSwipe(ev);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float dy = Math.abs((ev.getY() - mDownY));
                float dx = Math.abs((ev.getX() - mDownX));
                Log.e("message",dx+"--"+dy);
                //如果等于状态1,说明是滑动
               if(mTouchView!=null)
               {

               }
                break;
            case MotionEvent.ACTION_UP:
//                if (mTouchState == TOUCH_STATE_X) {
//                    if (mTouchView != null) {
////                        mTouchView.onSwipe(ev);
//                        //如果不是打开状态，那么就清空
//                        if (!mTouchView.isOpen()) {
//                            mTouchPosition = -1;
//                            mTouchView = null;
//                        }
//                    }
//                    if (mOnSwipeListener != null) {
//                        mOnSwipeListener.onSwipeEnd(mTouchPosition);
//                    }
//                    ev.setAction(MotionEvent.ACTION_CANCEL);
//                    super.onTouchEvent(ev);
//                    return true;
//                }


                break;
        }
        return super.onTouchEvent(ev);
    }
    private OnSwipeListener mOnSwipeListener;
    public static interface OnSwipeListener {
        void onSwipeStart(int position);
        void onSwipeEnd(int position);
    }
}
